前言:
銜接完成
程式碼:
//
// ViewController.swift
// Dog Breed Recognition App
//
// Created by PecaJo on 2021/6/24.
import UIKit
import FirebaseStorage
import FirebaseDatabase
class ViewController: UIViewController {
@IBOutlet weak var uploadButton: UIButton!
@IBOutlet weak var libraryButton: UIButton!
@IBOutlet weak var imageview: UIImageView!
@IBOutlet weak var dogNameLabel: UILabel!
@IBOutlet weak var dogAccLabel: UILabel!
let imagePicker = UIImagePickerController()
var ref: DatabaseReference! = Database.database().reference()
var tmpBool = false
var detect = "False"
override func viewDidLoad() {
super.viewDidLoad()
self.imagePicker.delegate = self
DispatchQueue.global().async {
while(true){
sleep(1)
if self.tmpBool {
self.uploadasync()
}
}
}
}
@IBAction func clickuploadbutton(_ sender: Any) {
self.ref.child("DetectBool/Bool/").observeSingleEvent(of: .value) {
(snapshot) in
self.detect = (snapshot.value as? String)!
}
if imageview.image != nil {
if detect == "False" {
uploadFile()
ref.child("DetectBool/Bool/").setValue("True")
ref.child("dogName/Name/").setValue("")
ref.child("dogAcc/Acc/").setValue("")
tmpBool = true
}
else{
let alertController = UIAlertController(title: "圖片偵測中...", message: "請稍後再試", preferredStyle: UIAlertController.Style.alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
present(alertController, animated: true, completion: nil)
}
}
else{
let alertController = UIAlertController(title: "圖片訊息", message: "請放入圖片", preferredStyle: UIAlertController.Style.alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
present(alertController, animated: true, completion: nil)
}
}
@IBAction func clicklibrarybutton(_ sender: Any) {
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true)
}
func uploadFile(){
let url = URL(string: "https://XXXXXXXXXXXXXXXXXXXXXX")
// Create a root reference
let storageRef = Storage.storage().reference()
// Data in memory
let imageData: Data = imageview.image!.jpegData(compressionQuality: 1.0)!
// Create a reference to the file you want to upload
let riversRef = storageRef.child("dog.jpg")
// Upload the file to the path "images/rivers.jpg"
let uploadTask = riversRef.putData(imageData, metadata: nil) { (metadata, error) in
guard let metadata = metadata else {
// Uh-oh, an error occurred!
return
}
// Metadata contains file metadata such as size, content-type.
let size = metadata.size
// You can also access to download URL after upload.
riversRef.downloadURL { (url, error) in
guard let downloadURL = url else {
// Uh-oh, an error occurred!
return
}
}
}
}
func uploadasync(){
DispatchQueue.main.async {
self.ref.child("dogName/Name/").observeSingleEvent(of: .value) {
(snapshot) in
if let name = snapshot.value as? String {
self.dogNameLabel.text = "\(name)"
}
}
self.ref.child("dogAcc/Acc/").observeSingleEvent(of: .value) {
(snapshot) in
if let acc = snapshot.value as? String {
self.dogAccLabel.text = "\(acc)"
}
}
}
}
}